//
//  DoctorInfo_ViewController.m
//  International_Hospital
//
//  Created by 李建强 on 16/7/6.
//  Copyright © 2016年 LiYiLin. All rights reserved.
//

#import "DoctorInfo_ViewController.h"
#import "conditTbaleViewController.h"
#import "IntroTableViewController.h"
#import "DoctorCell.h"
#import "conditTbaleViewController.h"
#import "IntroTableViewController.h"


#import <UIImageView+WebCache.h>
#import "LeftView.h"
#import "RightView.h"

@interface DoctorInfo_ViewController ()<UIScrollViewDelegate,UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong)UIView *topView;



@property (nonatomic,strong)IH_Label *nameLabel;

@property (nonatomic,strong)IH_Label *profLabel;

@property (nonatomic,strong)IH_Label *hospitalLabel;

@property (nonatomic,strong)UIImageView *addImageView;

@property (nonatomic,strong)IH_Label *addLabel;
@property (nonatomic,strong)UIImageView *qiImageView;

@property (nonatomic,strong)IH_Label *qiLabel;
@property (nonatomic,strong)UIImageView *flowerImageView;
@property (nonatomic,strong)IH_Label *flowerLabel;

@property (nonatomic,strong)UIView *grayView;

@property (nonatomic,strong)UIView *midView;

@property (nonatomic,strong)UIButton*conditionButton;

@property (nonatomic,strong)UIButton*introduceButton;

@property (nonatomic,strong)UIView *lineView;

@property (nonatomic,strong)UIView *horizontalLine;

@property (nonatomic,strong)UIScrollView *scrollView;
@property (nonatomic,strong)UITableView *leftView;
@property (nonatomic,strong)UITableView *rightView;

//更换为scrollView
//@property (nonatomic,strong)UICollectionView*collectionView;
//
//@property (nonatomic,strong)UICollectionViewFlowLayout *flowLayout;

@property (nonatomic,strong)UITableView*tableView;

@property (nonatomic,strong)UIView *minuteLine;

@property (nonatomic,strong)UIButton * bottomButton;

@property (nonatomic,strong)UIBarButtonItem *rightItem;

@property (nonatomic,assign)BOOL light;
@property (nonatomic,strong)NSArray *dataArray;

@property (nonatomic,strong)IntroTableViewController *introVc;
@property (nonatomic,strong)conditTbaleViewController *condiVc;


@property (nonatomic,assign)BOOL index;


@end

CGFloat KSize;

@implementation DoctorInfo_ViewController

static NSString *introID = @"intro";

static NSString *condID = @"condi";

-(NSArray *)dataArray{
    
    if (_dataArray == nil) {
        _dataArray = @[@"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7"];
    }
    return _dataArray;
}



static NSString*identifer = @"doctorcell";

- (void)viewDidLoad {
    [super viewDidLoad];
    [self checkIPhoneSize];
    [self setupUI];
    [self setupLayout];
    [self setupScrollView];

}
- (void)checkIPhoneSize {

    if (kDevice_Is_iPhone5) {
        KSize = 0.86;
    } else if (kDevice_Is_iPhone6) {
        KSize = 1;
    } else if (kDevice_Is_iPhone6Plus) {
        KSize = 1.07;
    }
}

-(void)setupUI{
    
    self.topView = [[UIView alloc]init];
    [self.view addSubview:self.topView];
    
    self.picImageView = [[UIImageView alloc]init];
    
    self.profLabel = [IH_Label labelWithTitel:@"血管瘤主治医师" withColor:[UIColor grayColor] addFont:[UIFont systemFontOfSize:14]];
    
    self.hospitalLabel = [IH_Label labelWithTitel:self.hospitalName withColor:[UIColor grayColor] addFont:[UIFont systemFontOfSize:14]];
    
    self.flowerImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"xianhua"]];
    
    self.flowerLabel = [IH_Label labelWithTitel:@"100" withColor:[UIColor colorWithRed:44/255.0 green:188/255.0 blue:185/255.0 alpha:1] addFont:[UIFont systemFontOfSize:14]];
    
    self.addLabel = [IH_Label labelWithTitel:@"100" withColor:[UIColor colorWithRed:44/255.0 green:188/255.0 blue:185/255.0 alpha:1] addFont:[UIFont systemFontOfSize:14]];
    self.addImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"yuyueliang"]];
    
    self.qiImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"jinqi"]];
    self.qiLabel = [IH_Label labelWithTitel:@"100" withColor:[UIColor colorWithRed:44/255.0 green:188/255.0 blue:185/255.0 alpha:1] addFont:[UIFont systemFontOfSize:14]];
    
    self.grayView = [[UIView alloc]init];
    self.grayView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    self.midView = [[UIView alloc]init];
    
    self.conditionButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.introduceButton = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [self.conditionButton setTitle:@"就诊条件" forState:UIControlStateNormal];
    [self.conditionButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [self.conditionButton setTitleColor:kThemeColor forState:UIControlStateSelected];
    self.conditionButton.selected =YES;
    
    [self.conditionButton addTarget:self action:@selector(didConitionClick) forControlEvents:UIControlEventTouchUpInside];
    
    self.lineView = [[UIView alloc]init];
    
    self.lineView.backgroundColor = [UIColor lightGrayColor];
    
    self.introduceButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.introduceButton setTitle:@"医生简介" forState:UIControlStateNormal];
    [self.introduceButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    
    [self.introduceButton addTarget:self action:@selector(didIntroClick) forControlEvents:UIControlEventTouchUpInside];
    
    [self.introduceButton setTitleColor:kThemeColor forState:UIControlStateSelected];
    self.horizontalLine = [[UIView alloc]init];
    
    self.horizontalLine.backgroundColor = kThemeColor;
    
    self.minuteLine = [[UIView alloc]init];
    
    self.minuteLine.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    self.bottomButton = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [self.bottomButton setTitle:@"咨询医生" forState:UIControlStateNormal];
    self.bottomButton.backgroundColor = kThemeColor;
    
    [self.bottomButton addTarget:self action:@selector(consultClick) forControlEvents:UIControlEventTouchUpInside];
    
    
    self.scrollView = [[UIScrollView alloc]init];
    self.scrollView.pagingEnabled =YES;
    self.scrollView.bounces = NO;
    
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.showsVerticalScrollIndicator = NO;
    self.scrollView.delegate = self;
    
    
    
    
    
    
    
    [self.topView addSubview:self.picImageView];
    [self.topView addSubview:self.nameLabel];
    [self.topView addSubview:self.profLabel];
    [self.topView addSubview:self.hospitalLabel];
    [self.topView addSubview:self.flowerImageView];
    [self.topView addSubview:self.flowerLabel];
    [self.topView addSubview:self.addImageView];
    [self.topView addSubview:self.addLabel];
    [self.topView addSubview:self.qiImageView];
    [self.topView addSubview:self.qiLabel];
    [self.view addSubview:self.grayView];
    [self.view addSubview:self.midView];
    [self.midView addSubview:self.conditionButton];
    [self.midView addSubview:self.introduceButton];
    [self.midView addSubview:self.lineView];
    [self.midView addSubview:self.introduceButton];
    [self.view addSubview:self.horizontalLine];
    [self.view addSubview:self.minuteLine];
    
    [self.view addSubview:self.bottomButton];
    
//    [self.view addSubview:self.collectionView];
    [self.view addSubview:self.scrollView];

    
    

    self.rightItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"kongxin"] style:UIBarButtonItemStylePlain target:self action:@selector(didRightItemClick)];
    
    if ([[NSUserDefaults standardUserDefaults]boolForKey:self.doctorName]) {
        BOOL light = [[NSUserDefaults standardUserDefaults]boolForKey:self.doctorName];
        self.light = light;
        if (light) {
            self.rightItem.tintColor = [UIColor redColor];
        }else{
            self.rightItem.tintColor = [UIColor whiteColor];
        }
        
    }else{
        
        self.rightItem.tintColor = [UIColor whiteColor];
    }
    self.navigationItem.rightBarButtonItem = self.rightItem;
}


#pragma --星星点灯
-(void)didRightItemClick{
    
    if (self.light) {
        
        self.rightItem.tintColor = [UIColor whiteColor];
        [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
        [SVProgressHUD setMinimumDismissTimeInterval:1.5];
        [SVProgressHUD showSuccessWithStatus:@"已取消关注"];
        self.light = NO;
        [[NSUserDefaults standardUserDefaults] setBool:NO forKey:self.doctorName];
        
    }else{
        self.rightItem.tintColor = [UIColor redColor];
        [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
        [SVProgressHUD setMinimumDismissTimeInterval:1.5];
        [SVProgressHUD showSuccessWithStatus:@"已关注"];
        self.light = YES;
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:self.doctorName];
    }
    
}



-(void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    
    if ([self.leftView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.leftView setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([self.leftView respondsToSelector:@selector(setLayoutMargins:)])  {
        [self.leftView setLayoutMargins:UIEdgeInsetsZero];
    }
    
     CGFloat scrollViewWidth = [UIScreen mainScreen].bounds.size.width;
    
    [self.leftView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.scrollView);
        make.top.equalTo(self.scrollView);
        make.width.mas_equalTo(self.view.frame.size.width);
        make.height.mas_equalTo(self.scrollView.frame.size.height);
        
    }];
    
    [self.rightView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.leftView.mas_right);
        make.top.equalTo(self.scrollView);
        make.width.mas_equalTo(self.view.frame.size.width);
        make.height.mas_equalTo(self.scrollView.frame.size.height);
    }];

    self.scrollView.contentSize = CGSizeMake(scrollViewWidth*2,0);

    if (self.banner.intValue == 0) {
        self.picImageView.image = [UIImage imageNamed:@"illness_img_person.png"];
    }else{
        [self.picImageView sd_setImageWithURL:[NSURL URLWithString:self.imgName]];
    }
    
    
//    [self setupCollectionViewLayout];
    self.nameLabel.text = self.doctorName;
    self.picImageView.layer.cornerRadius = 40;
    self.picImageView.layer.masksToBounds = YES;
}

-(void)setupLayout{
    
    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(64);
        make.left.offset(0);
        make.right.offset(0);
        make.height.mas_equalTo(150);
    }];
    [self.picImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.topView).offset(15);
        make.left.equalTo(self.topView).offset(15);
        make.width.mas_equalTo(80);
        make.height.mas_equalTo(80);
    }];
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.picImageView.mas_top).offset(10);
        make.left.equalTo(self.picImageView.mas_right).offset(10);
        
        
    }];
    
    [self.profLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.picImageView.mas_top).offset(10);
        make.left.equalTo(self.nameLabel.mas_right).offset(10);
        
    }];
    
    [self.hospitalLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.profLabel.mas_bottom).offset(30);
        make.left.equalTo(self.picImageView.mas_right).offset(10);
    }];
    
    [self.flowerImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.picImageView.mas_bottom).offset(30);
        make.centerX.equalTo(self.view);
        make.height.equalTo(self.flowerImageView.mas_height);
        make.width.equalTo(self.flowerImageView.mas_width);
        
    }];
    [self.flowerLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.flowerImageView.mas_top).offset(-1.5);
        make.left.equalTo(self.flowerImageView.mas_right).offset(5);
    }];
    
    
    
    [self.addImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(30);
        make.top.equalTo(self.flowerImageView.mas_top);
        make.height.equalTo(self.addImageView.mas_height);
        make.width.equalTo(self.addImageView.mas_width);
        
    }];
    
    [self.addLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.addImageView.mas_top).offset(-1.5);
        make.left.equalTo(self.addImageView.mas_right).offset(5);
        
    }];
    
    [self.qiLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.equalTo(self.view.mas_right).offset(-30);
        make.top.equalTo(self.flowerLabel.mas_top);
        
    }];
    
    [self.qiImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.qiLabel.mas_left).offset(-5);
        make.top.equalTo(self.flowerImageView.mas_top);
        make.height.equalTo(self.qiImageView.mas_height);
        make.width.equalTo(self.qiImageView.mas_width);
        
        
    }];
    
    [self.grayView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.offset(0);
        make.right.offset(0);
        make.top.equalTo(self.topView.mas_bottom).offset(0);
        make.height.mas_equalTo(3);
    }];
    
    [self.midView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(0);
        make.right.offset(0);
        make.top.equalTo(self.grayView.mas_bottom).offset(3);
        make.height.mas_equalTo(40);
    }];
    
    [self.conditionButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(30);
        
        make.width.mas_equalTo(100);
        make.centerY.equalTo(self.midView.mas_centerY);
        
        
    }];
    
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.midView.mas_top).offset(5);
        make.centerX.equalTo(self.midView.mas_centerX);
        make.width.mas_equalTo(2);
        make.bottom.equalTo(self.midView.mas_bottom).offset(-5);
    }];
    
    [self.introduceButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.offset(-30);
        
        make.width.mas_equalTo(100);
        make.centerY.equalTo(self.midView.mas_centerY);
    }];
    
    [self.horizontalLine mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.midView.mas_bottom).offset(10);
        make.left.equalTo(self.view.mas_left).offset(30);
        make.width.equalTo(self.conditionButton);
        make.height.mas_equalTo(2);
    }];
    
    
    [self.minuteLine mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.horizontalLine.mas_bottom).offset(10);
        make.left.equalTo(self.view).offset(0);
        make.right.equalTo(self.view);
        make.height.mas_equalTo(1);
    }];
    
    [self.bottomButton mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.bottom.equalTo(self.view).offset(0);
        make.left.right.equalTo(self.view).offset(0);
        make.height.mas_equalTo(50);
    }];
    
//    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
//       
//        make.top.equalTo(self.minuteLine.bottom);
//        make.bottom.equalTo(self.bottomButton.top);
//        make.left.equalTo(self.view);
//        make.right.equalTo(self.view);
//        
//    }];
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.minuteLine.bottom);
        make.bottom.equalTo(self.bottomButton.top);
        make.left.equalTo(self.view);
        make.right.equalTo(self.view);
        
    }];
    

    

}


-(void)setupScrollView{
    
    
    
    self.leftView = [[UITableView alloc]init];
    self.rightView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    
    
    [self.scrollView addSubview:self.leftView];
    [self.scrollView addSubview:self.rightView];
    
    self.rightView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.leftView.delegate =self;
    self.rightView.delegate =self;
    
    self.leftView.tag =1;
    self.rightView.tag =2;
    
    [self.leftView registerClass:[UITableViewCell class] forCellReuseIdentifier:introID];
    
    
    [self.rightView registerClass:[UITableViewCell class] forCellReuseIdentifier:introID];
    

    self.leftView.dataSource =self;
        self.rightView.dataSource =self;
}


-(void)didConitionClick{
    self.introduceButton.selected =NO;
    self.conditionButton.selected = YES;
    [self.scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
//    NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:0];
//    [self.scrollView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionNone animated:YES];
}


-(void)didIntroClick{
    

    self.introduceButton.selected = YES;
    self.conditionButton.selected = NO;
    [self.scrollView setContentOffset:CGPointMake([UIScreen mainScreen].bounds.size.width, 0) animated:YES];

    
}


-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPat{
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]){
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
}



-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    

    
    self.horizontalLine.transform = CGAffineTransformMakeTranslation(self.scrollView.contentOffset.x *0.58 * KSize, 0);
    if (self.scrollView.contentOffset.x <= self.scrollView.frame.size.width*0.4) {
        self.conditionButton.selected = YES;
        self.introduceButton.selected = NO;
    }else if (self.scrollView.contentOffset.x >= self.scrollView.frame.size.width*0.58 *0.5 * KSize){
        self.conditionButton.selected = NO;
        self.introduceButton.selected = YES;
    }
}

#pragma --咨询医生
-(void)consultClick{
    
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
    [SVProgressHUD setMinimumDismissTimeInterval:1.5];
    [SVProgressHUD showErrorWithStatus:@"功能暂未开发!"];
}




#pragma mark - 懒加载
-(IH_Label *)nameLabel{
    
    if (_nameLabel == nil) {
        _nameLabel = [[IH_Label alloc]init];
        _nameLabel.textColor = [UIColor blackColor];
    }
    return _nameLabel;
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (tableView.tag == 1) {
        
        return self.dataArray.count;
        
        
    }else{
        
        return 1;
    }
    
}
//
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    
    if (tableView.tag == 1) {
        
        return 1;
        
        
    }else{
        
        return self.dataArray.count;
    }
}
//
//


-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (tableView.tag ==1) {
        return 0.01;
    }
    if (section == 0) {
        return 0.01;
    }
    return 4;
}


-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (tableView.tag ==1) {
        return 0.01;
    }
    return 4;
}



-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:introID];
    
    if (tableView.tag == 1) {
        
        cell.textLabel.text= [NSString stringWithFormat:@"就诊条件:%@",self.dataArray[indexPath.row]];
//        cell.accessoryType = UITableViewCellAccessoryNone;
        
    }else{
        
        cell.textLabel.text = [NSString stringWithFormat:@"医生信息:%@",self.dataArray[indexPath.section]];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
 
    
    return cell;
    
}



-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [self.leftView deselectRowAtIndexPath:indexPath animated:YES];
    [self.rightView deselectRowAtIndexPath:indexPath animated:YES];
}


@end
